﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sector
{

    /// <summary>
    /// mAlpha 和 mTheta 扇形两点到原点的夹角
    /// </summary>
    public double mAlpha;
    public double mTheta;

    private double GetBeta
    {
        get
        {
            return mAlpha + mTheta;
        }

    }

    public Sector(double alphaRadian = 0, double betaRadian = 0)
    {
        mAlpha = alphaRadian;
        mTheta = betaRadian;
    }

    public void Clear()
    {
        mAlpha = mTheta = 0;
    }

    public void SetFullCircle()
    {
        mAlpha = 0;
        mTheta = Angle.TwoPI;
    }

    //cone 圆锥
    public void SetCone(Vector2 dir, double coneAngle, bool normalize)
    {
        mAlpha = Mathf.Atan2(dir.y, dir.x) - 0.5 * coneAngle;
        if (normalize)
            mAlpha = Angle.NormalizeRad2(mAlpha);

        mTheta = coneAngle;
    }

    //cy cx 扇形原点
    public void SetFromCoords(double cx, double cy, double ax, double ay, double bx, double by, bool normalize = false)
    {
        double newBeta = 0f;
        mAlpha = Mathf.Atan2((float)ay - (float)cy, (float)ax - (float)cx);
        newBeta = Mathf.Atan2((float)by - (float)cy, (float)bx - (float)cx);

        if (normalize)
        {
            mAlpha = Angle.NormalizeRad2(mAlpha);
            newBeta = Angle.NormalizeRad2(newBeta);
        }

        if (mAlpha >= newBeta)
            mAlpha = mTheta = 0;            //clear
        else
            mTheta = newBeta - mAlpha;
    }

    public void Copy(Sector a)
    {
        mAlpha = a.mAlpha;
        mTheta = a.mTheta;
    }

    /// <summary>
    /// 设置交集
    /// </summary>
    public void SetIntersection(Sector a, Sector b)
    {
        if (a.mTheta == 0 || b.mTheta == 0)
            mAlpha = mTheta = 0;                    //clear
        else
        {

            mAlpha = Math.Max(a.mAlpha, b.mAlpha);
            double newBeta = Math.Min(a.mAlpha + a.mTheta, b.mAlpha + b.mTheta);
            if (newBeta <= mAlpha)
                mAlpha = mTheta = 0;
            else
                mTheta = newBeta - mAlpha;
        }
    }

    public void SetUnion(Sector a, Sector b)
    {
        if (a.mTheta == 0)
        {
            mAlpha = b.mAlpha;
            mTheta = b.mTheta;
        }
        else if (b.mTheta == 0)
        {
            mAlpha = a.mAlpha;
            mTheta = a.mTheta;
        }
        else
        {
            mAlpha = Math.Min(a.mAlpha, b.mAlpha);
            double newBeta = Math.Max(a.GetBeta, b.GetBeta);
            if (newBeta <= mAlpha)
                mAlpha = mTheta = 0;        //clear
            else
                mTheta = newBeta - mAlpha;
        }
    }







}
